// stores/system.ts
import { defineStore } from 'pinia'
import type { 
  ThemeMode, 
  Language, 
  SystemState,
  SystemSummary
} from '#shared/types/system'
import {
  detectSystemTheme,
  getThemeClassName,
  getLanguageDisplayName
} from '#shared/utils/system'
// @ts-ignore
export const useSystemStore = defineStore('system', {
  state: (): SystemState => ({
    // 主题设置
    themeMode: 'default',
    isDarkMode: false,
    // 语言设置
    language: 'zh-CN'
  }),
  
  actions: {
    /**
     * 设置主题模式
     * @param mode 主题模式：default/v1/v2
     */
    setThemeMode(mode: ThemeMode) {
      this.themeMode = mode
    },
    
    /**
     * 设置暗色模式
     * @param isDark 是否启用暗色模式
     */
    setDarkMode(isDark: boolean) {
      this.isDarkMode = isDark
    },
    
    /**
     * 切换暗色模式
     */
    toggleDarkMode() {
      this.isDarkMode = !this.isDarkMode
    },
    
    /**
     * 切换主题模式
     */
    toggleTheme() {
      const modes: ThemeMode[] = ['default', 'v1', 'v2']
      const currentIndex = modes.indexOf(this.themeMode)
      const nextIndex = (currentIndex + 1) % modes.length
      const nextMode = modes[nextIndex]
      
      // 类型保护：确保nextMode不为undefined
      if (nextMode) {
        // @ts-ignore
        this.setThemeMode(nextMode)
      }
    },
    
    /**
     * 设置语言
     * @param lang 语言代码
     */
    setLanguage(lang: Language) {
      this.language = lang
    },
    
    /**
     * 重置所有设置为默认值
     */
    resetToDefaults() {
      this.themeMode = 'default'
      this.isDarkMode = false
      this.language = 'zh-CN'
    }
  },
  
  getters: {
    /**
     * 获取当前主题类名
     */
    themeClass: (state) => {
      return getThemeClassName(state.isDarkMode)
    },
    
    /**
     * 获取语言显示名称
     */
    languageDisplayName: (state) => {
      return getLanguageDisplayName(state.language)
    },
    
    /**
     * 获取系统状态摘要
     */
    systemSummary: (state): SystemSummary => {
      return {
        theme: state.themeMode,
        language: state.language
      }
    }
  },
  persist: true,
})
